package com.lrz.answerPlatform.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lrz.answerPlatform.model.dto.question.QuestionContentDTO;
import com.lrz.answerPlatform.model.entity.App;
import com.lrz.answerPlatform.model.entity.Question;
import com.lrz.answerPlatform.model.entity.ScoringResult;
import com.lrz.answerPlatform.model.entity.UserAnswer;
import com.lrz.answerPlatform.model.vo.QuestionVO;
import com.lrz.answerPlatform.service.QuestionService;
import com.lrz.answerPlatform.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;


/**
 * appType = 0 得分类app
 * scoringStrategy = 0 评分策略为自定义
 */
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    /**
     * 自定义得分类
     * @param choices
     * @param app
     * @return
     */
    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        //1.先拿到题目列表和结果信息
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, app.getId())
        );
        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, app.getId())
                        .orderByDesc(ScoringResult::getResultScoreRange)

        );

        //2.计算用户选项的最终分数
        //2.1 初始化总分数和最终结果
        int resultScore = 0;
        ScoringResult ScoreResult = scoringResultList.get(0);//初始化最终结果 默认为第一个
        //2.2 遍历题目列表
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();//获取题目列表
        for (QuestionContentDTO questionContentDTO : questionContent) {//遍历题目
            //2.3 遍历用户答案
            for (String answerChoice : choices) {
                //2.4 遍历某一个题目的选项
                for (QuestionContentDTO.Option option : questionContentDTO.getOptions()) {
                    // 如果用户的选项跟某一个题目中的某一个选项值一样，就加一分。（可能A是正确的，score为1；而B是错误的，score是0）
                    if (option.getKey().equals(answerChoice)) {
                        resultScore += option.getScore();
                    }
                }
            }
        }
        //3.遍历结果列表，根据最终分数判断属于哪一档
        for (ScoringResult scoringResult : scoringResultList) {
            if (resultScore >= scoringResult.getResultScoreRange()) {
                ScoreResult = scoringResult;
                break;
            }
        }
        //4.封装返回结果
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());

        userAnswer.setResultId(ScoreResult.getId());
        userAnswer.setResultName(ScoreResult.getResultName());
        userAnswer.setResultDesc(ScoreResult.getResultDesc());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultScore(resultScore);
        userAnswer.setResultPicture(ScoreResult.getResultPicture());
        return userAnswer;
    }
}
